package com.lanou.tk.tourism.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.entity.Discount;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class RecommendRvSpAdapter extends RecyclerView.Adapter<RecommendRvSpAdapter.LatestViewHolder>{

    protected List<Discount> discounts;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;


    public RecommendRvSpAdapter(Context c){
        queue= Volley.newRequestQueue(c);

    }

    public void addDatas(List<Discount> datas){
        discounts = datas;
        notifyDataSetChanged();
    }


        class LatestViewHolder extends RecyclerView.ViewHolder {
            private ImageView photo;
            private TextView title,price,priceoff;

        public LatestViewHolder(View v) {
            super(v);

            photo = (ImageView) v.findViewById(R.id.recommend_rv_sp_photo);
            title = (TextView) v.findViewById(R.id.recommend_rv_sp_title);
            price = (TextView) v.findViewById(R.id.recommend_rv_sp_price);
            priceoff = (TextView) v.findViewById(R.id.recommend_rv_sp_price0ff);
        }
    }

    @Override
    public int getItemCount() {
        return discounts != null && discounts.size() > 0 ? discounts.size() : 0;
    }




    @Override
    public void onBindViewHolder(LatestViewHolder holder, int position) {

        if(discounts != null && discounts.size() > 0){
            Discount discount = discounts.get(position);
           holder.title.setText(discount.getTitle());
            holder.price.setText(discount.getPrice());
            holder.priceoff.setText(discount.getPriceoff());

            String imgUrl = discount.getPhoto();
            getBitmap(imgUrl, holder.photo);

        }
    }
    public void getBitmap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);

            }
        }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);//将本次图片请求加入到队列中

    }









    @Override
    public LatestViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        //获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        View v = inflater.inflate(R.layout.fragment_recommend_shopping_recycler_view, null);

        return new LatestViewHolder(v);

    }

}
